package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.AddressBook;
import com.itheima.reggie.service.AddressBookService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "地址接口")
@Slf4j
@RestController
@RequestMapping("/addressBook")
public class AddressController {

    @Autowired
    private AddressBookService addressBookService;


    /**
     * 添加地址信息
     * @param addressBook
     * @return
     */
    @ApiOperation("添加地址信息")
    @PostMapping
    public R save(@RequestBody AddressBook addressBook) {
        log.info("添加地址信息的数据：addressBook = {}",addressBook);

        // 完善数据，需要将当前用户的id设置到地址中
        addressBook.setUserId(BaseContext.getCurrentId());

        addressBookService.save(addressBook);

        return R.success();
    }

    /**
     * 查询当前用户的地址信息
     * @return
     */
    @ApiOperation("查询当前用户的地址信息")
    @GetMapping("/list")
    public R<List<AddressBook>> list() {
        log.info("查询所有的地址信息");

        LambdaQueryWrapper<AddressBook> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(AddressBook::getUserId, BaseContext.getCurrentId());
        lambdaQueryWrapper.orderByDesc(AddressBook::getUpdateTime);

        List<AddressBook> addressBookList = addressBookService.list(lambdaQueryWrapper);

        return R.success(addressBookList);
    }


    /**
     * 根据id查询地址信息
     * @param id
     * @return
     */
    @ApiOperation("根据id查询地址信息")
    @ApiImplicitParam(name = "id",value = "地址id", dataTypeClass = Long.class)
    @GetMapping("/{id}")
    public R getById(@PathVariable Long id){
        log.info("根据id查询数据,id = {}", id);

        AddressBook addressBook = addressBookService.getById(id);

        if (addressBook != null) {
            return R.success(addressBook);
        }

        return R.error("数据未查询到！");

    }

    /**
     * 根据id修改数据
     * @param addressBook
     * @return
     */
    @ApiOperation("根据id修改数据")
    @PutMapping
    public R modify(@RequestBody AddressBook addressBook) {
        log.info("修改数据 addressRook = {}", addressBook);

        addressBookService.updateById(addressBook);

        return R.success();

    }

    /**
     * 根据id删除地址信息
     * @param ids
     * @return
     */
    @ApiOperation("根据id删除地址信息")
    @ApiImplicitParam(name = "ids", value = "地址id", dataTypeClass = Long.class)
    @DeleteMapping
    public R deleteById(Long ids) {
        log.info("根据id删除数据 ids = {}", ids);

        addressBookService.removeById(ids);

        return R.success();

    }


    /**
     * 修改地址的默认值
     * @param addressBook
     * @return
     */
    @ApiOperation("修改地址的默认值")
    @PutMapping("/default")
    public R updateByDefault(@RequestBody AddressBook addressBook) {
        log.info("需要设置默认值的数据 addressBook = {}",addressBook);

        addressBookService.updateByDefault(addressBook);

        return R.success();
    }

    /**
     * 查询默认地址
     * @return
     */
    @ApiOperation("查询默认地址")
    @GetMapping("/default")
    public R<AddressBook> getById(){
        log.info("查询默认地址");

        LambdaQueryWrapper<AddressBook> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(AddressBook::getUserId, BaseContext.getCurrentId());
        lambdaQueryWrapper.eq(AddressBook::getIsDefault, 1);

        AddressBook addressBook = addressBookService.getOne(lambdaQueryWrapper);

        return R.success(addressBook);

    }

}
